<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>ElasticSearch | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/62.a9d3b3b7.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>NoSQL</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/0a818f/" class="sidebar-link">Redis入门</a></li><li><a href="/pages/4c087b/" class="sidebar-link">MongoDB</a></li><li><a href="/pages/25c0d9/" aria-current="page" class="active sidebar-link">ElasticSearch</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#二、认识elasticsearch" class="sidebar-link">二、认识Elasticsearch</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_2-1-搜索引擎" class="sidebar-link">2.1 搜索引擎</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-1-1-概念" class="sidebar-link">2.1.1 概念</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-1-2-分类" class="sidebar-link">2.1.2 分类</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-1-3-发展趋势" class="sidebar-link">2.1.3 发展趋势</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-1-4-常见全文搜索引擎框架" class="sidebar-link">2.1.4 常见全文搜索引擎框架</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-1-5-选择" class="sidebar-link">2.1.5 选择</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_2-2-elasticsearch" class="sidebar-link">2.2 Elasticsearch</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-2-1-概念" class="sidebar-link">2.2.1 概念</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-2-2-定位" class="sidebar-link">2.2.2 定位</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_2-2-3-应用场景" class="sidebar-link">2.2.3 应用场景</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#三、elasticsearch安装" class="sidebar-link">三、Elasticsearch安装</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_3-1-服务端" class="sidebar-link">3.1 服务端</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_3-1-1-安装" class="sidebar-link">3.1.1 安装</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_3-1-2-版本说明" class="sidebar-link">3.1.2 版本说明</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_3-2-客户端" class="sidebar-link">3.2 客户端</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_3-2-1-elasticsearch-head插件" class="sidebar-link">3.2.1 Elasticsearch-head插件</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_3-2-2-kibana" class="sidebar-link">3.2.2 kibana</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_3-3-ik分词器" class="sidebar-link">3.3 IK分词器</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#四、elasticsearch核心概念" class="sidebar-link">四、Elasticsearch核心概念</a></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#五、索引操作" class="sidebar-link">五、索引操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_5-1-添加" class="sidebar-link">5.1 添加</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_5-1-1-无参数构造方式" class="sidebar-link">5.1.1 无参数构造方式</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_5-1-2-带类型type构造方式" class="sidebar-link">5.1.2 带类型type构造方式</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_5-2-查看" class="sidebar-link">5.2 查看</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_5-2-1-命令" class="sidebar-link">5.2.1 命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_5-2-2-es-head" class="sidebar-link">5.2.2 es-head</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_5-3-删除" class="sidebar-link">5.3 删除</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_5-3-1-命令" class="sidebar-link">5.3.1 命令</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_5-3-2-es-head" class="sidebar-link">5.3.2 es-head</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#六、数据类型" class="sidebar-link">六、数据类型</a></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#七、文档操作" class="sidebar-link">七、文档操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_7-1-添加" class="sidebar-link">7.1 添加</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_7-1-1-语法1-指定id" class="sidebar-link">7.1.1 语法1-指定ID</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_7-1-2-语法2-不指定id" class="sidebar-link">7.1.2 语法2-不指定ID</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_7-1-3-练习" class="sidebar-link">7.1.3 练习</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_7-2-更新" class="sidebar-link">7.2 更新</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_7-2-1-语法1-全量更新" class="sidebar-link">7.2.1 语法1-全量更新</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_7-2-2-语法2-部分更新" class="sidebar-link">7.2.2 语法2-部分更新</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_7-3-查询" class="sidebar-link">7.3 查询</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_7-4-删除" class="sidebar-link">7.4 删除</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#八、全文搜索" class="sidebar-link">八、全文搜索</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_8-1-数据准备" class="sidebar-link">8.1 数据准备</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_8-2-搜索语句" class="sidebar-link">8.2 搜索语句</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_8-2-1-语法1-match" class="sidebar-link">8.2.1 语法1-match</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_8-2-2-语法2-multi-match" class="sidebar-link">8.2.2 语法2-multi_match</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_8-3-中文分词器" class="sidebar-link">8.3 中文分词器</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_8-3-1-概念" class="sidebar-link">8.3.1 概念</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_8-3-2-分词策略" class="sidebar-link">8.3.2 分词策略</a></li><li class="sidebar-sub-header level4"><a href="/pages/25c0d9/#_8-3-3-分词词库" class="sidebar-link">8.3.3 分词词库</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_8-4-倒排索引" class="sidebar-link">8.4 倒排索引</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_8-5-高亮显示" class="sidebar-link">8.5 高亮显示</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#九、elasticsearch编程" class="sidebar-link">九、Elasticsearch编程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_9-1-crud操作" class="sidebar-link">9.1 CRUD操作</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_9-2-全文搜索" class="sidebar-link">9.2 全文搜索</a></li><li class="sidebar-sub-header level3"><a href="/pages/25c0d9/#_9-3-高亮显示-拓展" class="sidebar-link">9.3 高亮显示[拓展]</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/25c0d9/#十、小结与作业" class="sidebar-link">十、小结与作业</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=NoSQL" title="分类" data-v-06225672>NoSQL</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-12</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">ElasticSearch<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="elasticsearch"><a href="#elasticsearch" class="header-anchor">#</a> ElasticSearch</h1> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/elasticsearch.png" alt="img"></p> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <ul><li>认识Elasticsearch</li> <li>了解全文搜索引擎</li> <li>了解Elasticsearch安装与基本操作</li> <li>了解Elasticsearch文档CRUD操作</li> <li>了解Elasticsearch全文搜索</li> <li>了解SpringBoot集成Elasticsearch</li></ul> <h2 id="二、认识elasticsearch"><a href="#二、认识elasticsearch" class="header-anchor">#</a> 二、认识Elasticsearch</h2> <h3 id="_2-1-搜索引擎"><a href="#_2-1-搜索引擎" class="header-anchor">#</a> 2.1 搜索引擎</h3> <h4 id="_2-1-1-概念"><a href="#_2-1-1-概念" class="header-anchor">#</a> 2.1.1 概念</h4> <p>所谓搜索引擎，就是根据用户需求与一定算法，运用特定策略从互联网检索出指定信息反馈给用户的一门检索技术。搜索引擎依托于多种技术，如网络爬虫技术、检索排序技术、网页处理技术、大数据处理技术、自然语言处理技术等，为信息检索用户提供快速、高相关性的信息服务。搜索引擎技术的核心模块一般包括爬虫、索引、检索和排序等，同时可添加其他一系列辅助模块，以为用户创造更好的网络使用环境。</p> <h4 id="_2-1-2-分类"><a href="#_2-1-2-分类" class="header-anchor">#</a> 2.1.2 分类</h4> <p><strong>全文搜索引擎</strong></p> <p>一般网络用户适用于全文搜索引擎。这种搜索方式方便、简捷，并容易获得所有相关信息。但搜索到的信息过于庞杂，因此用户需要逐一浏览并甄别出所需信息。尤其在用户没有明确检索意图情况下，这种搜索方式非常有效。</p> <p><strong>元搜索引擎</strong></p> <p>元搜索引擎适用于广泛、准确地收集信息。不同的全文搜索引擎由于其性能和信息反馈能力差异，导致其各有利弊。元搜索引擎的出现恰恰解决了这个问题，有利于各基本搜索引擎间的优势互补。而且本搜索方式有利于对基本搜索方式进行全局控制，引导全文搜索引擎的持续改善。</p> <p><strong>垂直搜索引擎</strong></p> <p>垂直搜索引擎适用于有明确搜索意图情况下进行检索。例如，用户购买机票、火车票、汽车票时，或想要浏览网络视频资源时，都可以直接选用行业内专用搜索引擎，以准确、迅速获得相关信息。</p> <p><strong>目录搜索引擎</strong></p> <p>目录搜索引擎是网站内部常用的检索方式。本搜索方式指在对网站内信息整合处理并分目录呈现给用户，但其缺点在于用户需预先了解本网站的内容，并熟悉其主要模块构成。总而观之，目录搜索方式的适应范围非常有限，且需要较高的人工成本来支持维护。</p> <p><strong>新网页搜索引擎</strong></p> <p>2022年6月3日消息，苹果将推出以用户为中心的新网页搜索引擎。</p> <h4 id="_2-1-3-发展趋势"><a href="#_2-1-3-发展趋势" class="header-anchor">#</a> 2.1.3 发展趋势</h4> <p><strong>1、社会化搜索</strong></p> <p>社交网络平台和应用占据了互联网的主流，社交网络平台强调用户之间的联系和交互，这对传统的搜索技术提出了新的挑战。</p> <p>传统搜索技术强调搜索结果和用户需求的相关性，社会化搜索除了相关性外，还额外增加了一个维度，即搜索结果的可信赖性。对某个搜索结果，传统的结果可能成千上万，但如果处于用户社交网络内其他用户发布的信息、点评或验证过的信息则更容易信赖，这是与用户的心里密切相关的。社会化搜索为用户提供更准确、更值得信任的搜索结果。</p> <p><strong>2、实时搜索</strong></p> <p>对搜索引擎的实时性要求日益增高，这也是搜索引擎未来的一个发展方向。</p> <p>实时搜索最突出的特点是时效性强，越来越多的突发事件首次发布在微博上，实时搜索核心强调的就是“快”，用户发布的信息第一时间能被搜索引擎搜索到。不过在国内，实时搜索由于各方面的原因无法普及使用，比如Google的实时搜索是被重置的，百度也没有明显的实时搜索入口。</p> <p><strong>3、移动搜索</strong></p> <p>随着智能手机的快速发展，基于手机的移动设备搜索日益流行，但移动设备有很大的局限性，比如屏幕太小，可显示的区域不多，计算资源能力有限，打开网页速度很慢，手机输入繁琐等问题都需要解决。</p> <p>目前，随着智能手机的快速普及，移动搜索一定会更加快速的发展，所以移动搜索的市场占有率会逐步上升，而对于没有移动版的网站来说，百度也提供了“百度移动开放平台”来弥补这个缺失。</p> <p><strong>4、个性化搜索</strong></p> <p>个性化搜索主要面临两个问题：如何建立用户的个人兴趣模型?在搜索引擎里如何使用这种个人兴趣模型? [3]</p> <p>个性化搜索的核心是根据用户的网络行为，建立一套准确的个人兴趣模型。而建立这样一套模型，就要全民收集与用户相关的信息，包括用户搜索历史、点击记录、浏览过的网页、用户E-mail信息、收藏夹信息、用户发布过的信息、博客、微博等内容。比较常见的是从这些信息中提取出关键词及其权重。为不同用户提供个性化的搜索结果，是搜索引擎总的发展趋势，但现有技术有很多问题，比如个人隐私的泄露，而且用户的兴趣会不断变化，太依赖历史信息，可能无法反映用户的兴趣变化。</p> <p><strong>5、地理位置感知搜索</strong></p> <p>目前，很多手机已经有GPS的应用了，这是基于地理位置感知的搜索，而且可以通过陀螺仪等设备感知用户的朝向，基于这种信息，可以为用户提供准确的地理位置服务以及相关搜索服务。目前，此类应用已经大行其道，比如手机地图APP。</p> <p><strong>6、跨语言搜索</strong></p> <p>如何将中文的用户查询翻译为英文查询，目前主流的方法有3种：机器翻译、双语词典查询和双语语料挖掘。对于一个全球性的搜索引擎来说，具备跨语言搜索功能是必然的发展趋势，而其基本的技术路线一般会采用查询翻译加上网页的机器翻译这两种技术手段。</p> <p><strong>7、多媒体搜索</strong></p> <p>目前，搜索引擎的查询还是基于文字的，即使是图片和视频搜索也是基于文本方式。那么未来的多媒体搜索技术则会弥补查询这一缺失。多媒体形式除了文字，主要包括图片、音频、视频。多媒体搜索比纯文本搜索要复杂许多，一般多媒体搜索包含4个主要步骤：多媒体特征提取、多媒体数据流分割、多媒体数据分类和多媒体数据搜索引擎。</p> <p><strong>8、情境搜索</strong></p> <p>情境搜索是融合了多项技术的产品，上面介绍的社会化搜索、个性化搜索、地点感知搜索等都是支持情境搜索的，目前Google在大力提倡这一概念。所谓情境搜索，就是能够感知人与人所处的环境，针对“此时此地此人”来建立模型，试图理解用户查询的目的，根本目标还是要理解人的信息需求。比如某个用户在苹果专卖店附近发出“苹果”这个搜索请求，基于地点感知及用户的个性化模型，搜索引擎就有可能认为这个查询是针对苹果公司的产品，而非对水果的需求。</p> <h4 id="_2-1-4-常见全文搜索引擎框架"><a href="#_2-1-4-常见全文搜索引擎框架" class="header-anchor">#</a> 2.1.4 常见全文搜索引擎框架</h4> <p>主要介绍13款现有的开源搜索引擎，你可以将它们用在你的项目中以实现检索功能。</p> <p>原文：https://www.open-open.com/lib/view/open1433383682182.html</p> <ul><li><p>Lucene</p> <p>Lucene的开发语言是Java，也是Java家族中最为出名的一个开源搜索引擎，在Java世界中已经是标准的全文检索程序，它提供了完整的查询引擎和索引引擎，没有中文分词引擎，需要自己去实现，因此用Lucene去做一个搜素引擎需要自己去架构.另外它不支持实时搜索，但linkedin和 推ter有分别对Lucene改进的实时搜素. 其中Lucene有一个C++移植版本叫CLucene，CLucene因为使用C++编写，所以理论上要比lucene快.</p> <p>官方主页：http://lucene.apache.org/</p> <p>Lucene官方主页：http://sourceforge.net/projects/clucene/</p></li> <li><p>Sphinx</p> <p>Sphinx是一个用C++语言写的开源搜索引擎，也是现在比较主流的搜索引擎之一，在建立索引的事件方面比Lucene快50%，但是索引文件比 Lucene要大一倍，因此Sphinx在索引的建立方面是空间换取事件的策略，在检索速度上，和lucene相差不大，但检索精准度方面Lucene要优于Sphinx，另外在加入中文分词引擎难度方面，Lucene要优于Sphinx.其中Sphinx支持实时搜索，使用起来比较简单方便.</p> <p>官方主页：http://sphinxsearch.com/about/sphinx/</p></li> <li><p>Xapian</p> <p>Xapian是一个用C++编写的全文检索程序，它的api和检索原理和lucene在很多方面都很相似，算是填补了lucene在C++中的一个空缺.</p> <p>官方主页：http://xapian.org/</p></li> <li><p>Nutch</p> <p>Nutch是一个用java实现的开源的web搜索引擎，包括爬虫crawler，索引引擎，查询引擎. 其中Nutch是基于Lucene的，Lucene为Nutch提供了文本索引和搜索的API.</p> <p>对于应该使用Lucene还是使用Nutch，应该是如果你不需要抓取数据的话，应该使用Lucene，最常见的应用是：你有数据源，需要为这些数据提供一个搜索页面，在这种情况下，最好的方式是直接从数据库中取出数据，并用Lucene API建立索引.</p> <p>官方主页：http://nutch.apache.org/</p></li> <li><p>DataparkSearch</p> <p>DataparkSearch是一个用C语言实现的开源的搜索引擎. 其中网页排序是采用神经网络模型.  其中支持HTTP，HTTPS，FTP，NNTP等下载网页.包括索引引擎，检索引擎和中文分词引擎(这个也是唯一的一个开源的搜索引擎里有中文分词引擎).能个性化定制搜索结果，拥有完整的日志记录.</p> <p>官方主页：http://www.dataparksearch.org/</p></li> <li><p>Zettair</p> <p>Zettair是根据Justin Zobel的研究成果为基础的全文检索实验系统.它是用C语言实现的. 其中Justin Zobel在全文检索领域很有名气，是业界第一个系统提出倒排序索引差分压缩算法的人，倒排列表的压缩大大提高了检索和加载的性能，同时空间膨胀率也缩小到相当优秀的水平. 由于Zettair是源于学术界，代码是由RMIT University的搜索引擎组织写的，因此它的代码简洁精炼，算法高效，是学习倒排索引经典算法的非常好的实例. 其中支持linux，windows，mac os等系统.</p> <p>官方主页：http://www.seg.rmit.edu.au/zettair/about.html</p></li> <li><p>Indri</p> <p>Indri是一个用C语言和C++语言写的全文检索引擎系统，是由University of Massachusetts和Carnegie Mellon University合作推出的一个开源项目. 特点是跨平台，API接口支持Java，PHP，C++.</p> <p>官方主页：http://www.lemurproject.org/indri/</p></li> <li><p>Terrier</p> <p>Terrier是由School of Computing Science，Universityof Glasgow用java开发的一个全文检索系统.</p> <p>官方主页：http://terrier.org/</p></li> <li><p>Galago</p> <p>Galago是一个用java语言写的关于文本搜索的工具集. 其中包括索引引擎和查询引擎，还包括一个叫TupleFlow的分布式计算框架(和google的MapReduce很像).这个检索系统支持很多Indri查询语言.</p> <p>官方主页：http://www.galagosearch.org/</p></li> <li><p>Zebra</p> <p>Zebra是一个用C语言实现的检索程序，特点是对大数据的支持，支持EMAIL，XML，MARC等格式的数据.</p> <p>官方主页：https://www.indexdata.com/zebra</p></li> <li><p>Solr</p> <p>Solr是一个用java开发的独立的企业级搜索应用服务器，它提供了类似于Web-service的API接口，它是基于Lucene的全文检索服务器，也算是Lucene的一个变种，很多一线互联网公司都在使用Solr，也算是一种成熟的解决方案.</p> <p>官方主页：http://lucene.apache.org/solr/</p></li> <li><p>Elasticsearch</p> <p>Elasticsearch是一个采用java语言开发的，基于Lucene构造的开源，分布式的搜索引擎. 设计用于云计算中，能够达到实时搜索，稳定可靠. Elasticsearch的数据模型是JSON.</p> <p>官方主页：http://www.elasticsearch.org/</p></li> <li><p>Whoosh</p> <p>Whoosh是一个用纯python写的开源搜索引擎.</p> <p>官方主页：https://bitbucket.org/mchaput/whoosh/wiki/Home</p></li></ul> <h4 id="_2-1-5-选择"><a href="#_2-1-5-选择" class="header-anchor">#</a> 2.1.5 选择</h4> <p><strong>Lucene</strong></p> <p>Lucene的开发语言是Java，也是Java家族中最为出名的一个开源搜索引擎，在Java世界中已经是标准的全文检索程序，它提供了完整的查询引擎和索引引擎，没有中文分词引擎，需要自己去实现，因此用Lucene去做一个搜素引擎需要自己去架构，另外它不支持实时搜索。但是solr和elasticsearch都是基于Lucene封装。</p> <p>1.2 优点</p> <p>成熟的解决方案，有很多的成功案例。apache 顶级项目，正在持续快速的进步。庞大而活跃的开发社区，大量的开发人员。它只是一个类库，有足够的定制和优化空间：经过简单定制，就可以满足绝大部分常见的需求；经过优化，可以支持 10亿+ 量级的搜索。</p> <p>1.3 缺点</p> <p>需要额外的开发工作。所有的扩展，分布式，可靠性等都需要自己实现；非实时，从建索引到可以搜索中间有一个时间延迟，而当前的“近实时”(Lucene Near Real Time search)搜索方案的可扩展性有待进一步完善.</p> <p><strong>Apache Solr</strong></p> <p>Solr是一个高性能，采用Java开发，基于Lucene的全文搜索服务器。文档通过Http利用XML加到一个搜索集合中。查询该集合也是通过 http收到一个XML/JSON响应来实现。它的主要特性包括：高效、灵活的缓存功能，垂直搜索功能，高亮显示搜索结果，通过索引复制来提高可用性，提 供一套强大Data Schema来定义字段，类型和设置文本分析，提供基于Web的管理界面等。</p> <p>2.2 优点</p> <p>（1）Solr有一个更大、更成熟的用户、开发和贡献者社区。</p> <p>（2）支持添加多种格式的索引，如：HTML、PDF、微软 Office 系列软件格式以及 JSON、XML、CSV 等纯文本格式。</p> <p>（3）Solr比较成熟、稳定。</p> <p>（4）不考虑建索引的同时进行搜索，速度更快。</p> <p>2.3 缺点</p> <p>建立索引时，搜索效率下降，实时索引搜索效率不高</p> <p><strong>ElasticSearch</strong></p> <p>ElasticSearch是一个基于Lucene构建的开源，分布式，RESTful搜索引擎。设计用于云计算中，能够达到实时搜索，稳定，可靠，快速，安装使用方便。<strong>支持通过HTTP使用JSON进行数据索引。</strong></p> <p>3.2 优点</p> <p>（1）Elasticsearch是分布式的。不需要其他组件，分发是实时的，被叫做”Push replication”。</p> <p>（2）Elasticsearch 完全支持 Apache Lucene 的接近实时的搜索。</p> <p>（3）处理多租户（multitenancy）不需要特殊配置，而Solr则需要更多的高级设置。</p> <p>（4）Elasticsearch 采用 Gateway 的概念，使得完备份更加简单。</p> <p>各节点组成对等的网络结构，某些节点出现故障时会自动分配其他节点代替其进行工作。</p> <p>3.3 缺点</p> <p>还不够自动（不适合当前新的Index Warmup API）   索引预热API</p> <p>究竟选哪一个呢？</p> <p><strong>ES or Solr</strong></p> <p>这里有篇文章做了很详细的比对：https://www.cnblogs.com/jajian/p/9801154.html</p> <table><thead><tr><th style="text-align:center;">特征</th> <th style="text-align:center;">Solr/SolrCloud</th> <th style="text-align:center;">Elasticsearch</th></tr></thead> <tbody><tr><td style="text-align:center;">社区和开发者</td> <td style="text-align:center;">Apache 软件基金和社区支持</td> <td style="text-align:center;">单一商业实体及其员工</td></tr> <tr><td style="text-align:center;">节点发现</td> <td style="text-align:center;">Apache Zookeeper，在大量项目中成熟且经过实战测试</td> <td style="text-align:center;">Zen内置于Elasticsearch本身，需要专用的主节点才能进行分裂脑保护</td></tr> <tr><td style="text-align:center;">碎片放置</td> <td style="text-align:center;">本质上是静态，需要手动工作来迁移分片，从Solr 7开始 - Autoscaling API允许一些动态操作</td> <td style="text-align:center;">动态，可以根据群集状态按需移动分片</td></tr> <tr><td style="text-align:center;">高速缓存</td> <td style="text-align:center;">全局，每个段更改无效</td> <td style="text-align:center;">每段，更适合动态更改数据</td></tr> <tr><td style="text-align:center;">分析引擎性能</td> <td style="text-align:center;">非常适合精确计算的静态数据</td> <td style="text-align:center;">结果的准确性取决于数据放置</td></tr> <tr><td style="text-align:center;">全文搜索功能</td> <td style="text-align:center;">基于Lucene的语言分析，多建议，拼写检查，丰富的高亮显示支持</td> <td style="text-align:center;">基于Lucene的语言分析，单一建议API实现，高亮显示重新计算</td></tr> <tr><td style="text-align:center;">DevOps支持</td> <td style="text-align:center;">尚未完全，但即将到来</td> <td style="text-align:center;">非常好的API</td></tr> <tr><td style="text-align:center;">非平面数据处理</td> <td style="text-align:center;">嵌套文档和父-子支持</td> <td style="text-align:center;">嵌套和对象类型的自然支持允许几乎无限的嵌套和父-子支持</td></tr> <tr><td style="text-align:center;">查询DSL</td> <td style="text-align:center;">JSON（有限），XML（有限）或URL参数</td> <td style="text-align:center;">JSON</td></tr> <tr><td style="text-align:center;">索引/收集领导控制</td> <td style="text-align:center;">领导者安置控制和领导者重新平衡甚至可以节点上的负载</td> <td style="text-align:center;">不可能</td></tr> <tr><td style="text-align:center;">机器学习</td> <td style="text-align:center;">内置 - 在流聚合之上，专注于逻辑回归和学习排名贡献模块</td> <td style="text-align:center;">商业功能，专注于异常和异常值以及时间序列数据</td></tr></tbody></table> <p><strong>流行程度来看</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1162587-20181020012622260-1901329745.png" alt="img">*</p> <p>根据上面综合分析，最终选择：<strong>Elasticsearch</strong></p> <h3 id="_2-2-elasticsearch"><a href="#_2-2-elasticsearch" class="header-anchor">#</a> 2.2 Elasticsearch</h3> <h4 id="_2-2-1-概念"><a href="#_2-2-1-概念" class="header-anchor">#</a> 2.2.1 概念</h4> <p>**ElasticSearch是一个基于Lucene的搜索服务器。**它提供了一个分布式多用户能力的全文搜索引擎，<strong>基于RESTful web接口</strong>。Elasticsearch是用Java开发的，并作为Apache许可条款下的开放源码发布，是当前流行的企业级搜索引擎。设计用于云计算中，能够达到实时搜索，稳定，可靠，快速，安装使用方便。</p> <p>1&gt;基于Apache Lucene 构建的开源搜索引擎</p> <p>2&gt;才有java编写的，提供简单易用的RESTFul API</p> <p>3&gt;轻松的横向拓展， 可以支持PB级的结构化或非结构化数据处理</p> <p>在Java、.NET（C#）、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示，Elasticsearch是最受欢迎的企业搜索引擎，其次是Apache Solr，也是基于Lucene。</p> <h4 id="_2-2-2-定位"><a href="#_2-2-2-定位" class="header-anchor">#</a> 2.2.2 定位</h4> <p>狭义的上的理解：</p> <p><strong>专注于 搜索 的 非关系型  数据库</strong></p> <p>类似于mongodb</p> <h4 id="_2-2-3-应用场景"><a href="#_2-2-3-应用场景" class="header-anchor">#</a> 2.2.3 应用场景</h4> <p>多条件搜索，<strong>站内搜索</strong>，地理位置搜索, <strong>elk</strong></p> <h2 id="三、elasticsearch安装"><a href="#三、elasticsearch安装" class="header-anchor">#</a> 三、Elasticsearch安装</h2> <p>对Elasticsearch有了大体之后，我们看下如何实现Elasticsearch的安装。</p> <p>安装相关文件下载路径：https://elasticsearch.cn/download/</p> <p>本教程使用的Elasticsearch 是 7.10.2</p> <h3 id="_3-1-服务端"><a href="#_3-1-服务端" class="header-anchor">#</a> 3.1 服务端</h3> <h4 id="_3-1-1-安装"><a href="#_3-1-1-安装" class="header-anchor">#</a> 3.1.1 安装</h4> <p><strong>步骤1：下载Elasticsearch安装包</strong></p> <p>下载Elasticsearch版本：elasticsearch-7.10.2-windows-x86_64.zip</p> <p><strong>步骤2：检查Elasticsearch安装环境</strong></p> <p>Elasticsearch是使用Java编写的解压即用的软件，安装是需要有Java的运行环境即可。</p> <p>检查是否配置了java环境：</p> <p>打开cmd命令框，输入： set java_home</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905163431188.png" alt="image-20220905163431188">*</p> <p><strong>步骤3：启动Elasticsearch程序</strong></p> <p>环境检查后，把压缩包解压后，进入到bin目录运行elasticsearch.bat</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905163852385.png" alt="image-20220905163852385">*</p> <p><strong>步骤4：输入地址，检查</strong></p> <p>浏览器输入：http://localhost:9200，看到浏览器输出服务器的信息，表示安装成功，可以使用了</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905163923410.png" alt="image-20220905163923410">*</p> <h4 id="_3-1-2-版本说明"><a href="#_3-1-2-版本说明" class="header-anchor">#</a> 3.1.2 版本说明</h4> <p>Elasticsearch 目前拥有的版本：1.x  2.x      5.x   6.x  7.x  8.x</p> <p>留意看，发现没有3.x   4.x 这是为何？？</p> <p>原因：ELK技术组合</p> <p>ELK是官方主导的一个非常好用的日志监控技术组合，有点类似SSM组合。其中</p> <p><strong>E  --&gt;Elasticsearch</strong>是一个高度可扩展的全文搜索和分析引擎，基于Apache Lucence（事实上，Lucence也是百度所采用的搜索引擎）构建，能够对大容量的数据进行接近实时的存储、搜索和分析操作。</p> <p><strong>L ---&gt;Logstash</strong>是一个数据收集引擎，它可以动态的从各种数据源搜集数据，并对数据进行过滤、分析和统一格式等操作，并将输出结果存储到指定位置上。Logstash支持普通的日志文件和自定义Json格式的日志解析。</p> <p><strong>K---&gt;Kibana</strong>是一个数据分析和可视化平台，通常与Elasticsearch配合使用，用于对其中的数据进行搜索、分析，并且以统计图标的形式展示。</p> <p><strong>它们工作流程：</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/e1caef2f26814ae09315aff7ff1c0577.png" alt="在这里插入图片描述"></p> <p>总结：ELK技术组合时，要求版本必须一致，否则报错， 当初Elasticsearch跟上面lk组合时，版本才到2.x，后续同一个升级，从5.x开始。</p> <h3 id="_3-2-客户端"><a href="#_3-2-客户端" class="header-anchor">#</a> 3.2 客户端</h3> <p>本教程使用的客户端有两个： 1&gt;Elasticsearch-head插件   2&gt;kibana</p> <p>为啥用2个？</p> <p>原因：</p> <p>Elasticsearch-head插件 查看Elasticsearch数据非常方便，直接上手，kibana 查看数据相对麻烦</p> <p>Elasticsearch-head插件 操作Elasticsearch命令非常不友好，跟记事本一样，没有任何代码提示与高亮显示，但是kibana有</p> <p><strong>综合起来：使用Elasticsearch-head插件 的数据查询  + kibana 命令操作</strong></p> <h4 id="_3-2-1-elasticsearch-head插件"><a href="#_3-2-1-elasticsearch-head插件" class="header-anchor">#</a> 3.2.1 Elasticsearch-head插件</h4> <p>Elasticsearch-head插件 是一个chrome浏览器插件，直接安装离线插件即可</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905170052225.png" alt="image-20220905170052225"></p> <p>显式效果</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905172359256.png" alt="image-20220905172359256">*</p> <h4 id="_3-2-2-kibana"><a href="#_3-2-2-kibana" class="header-anchor">#</a> 3.2.2 kibana</h4> <p>kibana是一个大的组件，安装需要满足几个条件</p> <p>1&gt;安装路径必须是英文(非中文)</p> <p>2&gt;安装路径不允许有空格</p> <p>kibana是一个绿色软件，解压即可，启动时，执行bin/kibana.bat即可</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905170259049.png" alt="image-20220905170259049">*</p> <blockquote><p><strong>注意：启动kibana前，先启动Elasticsearch</strong></p></blockquote> <p>启动成功之后访问路径： http://localhost:5601</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905170757205.png" alt="image-20220905170757205"></p> <h3 id="_3-3-ik分词器"><a href="#_3-3-ik分词器" class="header-anchor">#</a> 3.3 IK分词器</h3> <p>当前暂时还没有用到IK分词器，后面讲全文搜索时，才会涉及到分词器的逻辑，这里是顺手装上而已。</p> <p>将分词安装包复制到Elasticsearch安装目录的plugins文件夹</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905171314428.png" alt="image-20220905171314428">*</p> <p><strong>注意： ik分词器安装完成之后，必须重启Elastsearch才能生效。</strong></p> <h2 id="四、elasticsearch核心概念"><a href="#四、elasticsearch核心概念" class="header-anchor">#</a> 四、Elasticsearch核心概念</h2> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905171430202.png" alt="image-20220905171430202"></p> <p><strong>注意：Elasticsearch约定一个索引只能有一个类型type，并且类型名固定为(_doc)</strong></p> <h2 id="五、索引操作"><a href="#五、索引操作" class="header-anchor">#</a> 五、索引操作</h2> <h3 id="_5-1-添加"><a href="#_5-1-添加" class="header-anchor">#</a> 5.1 添加</h3> <h4 id="_5-1-1-无参数构造方式"><a href="#_5-1-1-无参数构造方式" class="header-anchor">#</a> 5.1.1 无参数构造方式</h4> <p><strong>语法</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>PUT my_index
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>练习</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求：创建名为my_index的索引
PUT my_index
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="_5-1-2-带类型type构造方式"><a href="#_5-1-2-带类型type构造方式" class="header-anchor">#</a> 5.1.2 带类型type构造方式</h4> <p><strong>语法</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>PUT /索引名
{
  &quot;mappings&quot;: {
      &quot;properties&quot;: {
        字段名: {
          &quot;type&quot;: 字段类型, 
          &quot;analyzer&quot;: 分词器类型, 
          &quot;search_analyzer&quot;: 分词器类型,
          ...
        },
        ...
      }
    }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p><strong>练习</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求：创建名为my_index的索引
PUT /my_index
{
  &quot;mappings&quot;: {  
      &quot;properties&quot;:{
         &quot;id&quot;:{
           &quot;type&quot;:&quot;long&quot;
         },
         &quot;name&quot;:{
           &quot;type&quot;:&quot;keyword&quot;
         },
         &quot;age&quot;:{
           &quot;type&quot;:&quot;integer&quot;
         }
      }
    }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p><strong>注意：</strong></p> <p>1：索引不能有大写字母</p> <p>2：参数格式必须是标准的json格式</p> <h3 id="_5-2-查看"><a href="#_5-2-查看" class="header-anchor">#</a> 5.2 查看</h3> <h4 id="_5-2-1-命令"><a href="#_5-2-1-命令" class="header-anchor">#</a> 5.2.1 命令</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#看单个
GET  /索引名 
#看所有
GET _cat/indices
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="_5-2-2-es-head"><a href="#_5-2-2-es-head" class="header-anchor">#</a> 5.2.2 es-head</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905172626189.png" alt="image-20220905172626189">*</p> <h3 id="_5-3-删除"><a href="#_5-3-删除" class="header-anchor">#</a> 5.3 删除</h3> <h4 id="_5-3-1-命令"><a href="#_5-3-1-命令" class="header-anchor">#</a> 5.3.1 命令</h4> <div class="language-json line-numbers-mode"><pre class="language-json"><code>#语法：DELETE /索引名
DELETE /my_index
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="_5-3-2-es-head"><a href="#_5-3-2-es-head" class="header-anchor">#</a> 5.3.2 es-head</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905172818580.png" alt="image-20220905172818580">*</p> <h2 id="六、数据类型"><a href="#六、数据类型" class="header-anchor">#</a> 六、数据类型</h2> <p>Elasticsearch 是使用Java开发出来的，所以它很多类型给Java是一样的，比如：</p> <p>byte  short  integer  double float date boolean binary array object ip</p> <p>这里有个比较特殊的类型： String， Elasticsearch 中String类型有种存在方式：</p> <p><strong>text 类型</strong>：当一个字段是要被<strong>全文搜索</strong>的，比如Email内容、产品描述，应该使用text类型。设置text类型以后，字段内容会被分析，在生成倒排索引以前，字符串会被分析器分成一个一个词项。text类型的字段不用于排序，很少用于聚合。</p> <p><strong>keyword类型：<strong>适用于索引结构化的字段，比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过</strong>精确值搜索</strong>到。</p> <p><strong>后续使用Elasticsearch 做全文搜索时，要求条件列必须是：text类型。</strong></p> <h2 id="七、文档操作"><a href="#七、文档操作" class="header-anchor">#</a> 七、文档操作</h2> <h3 id="_7-1-添加"><a href="#_7-1-添加" class="header-anchor">#</a> 7.1 添加</h3> <h4 id="_7-1-1-语法1-指定id"><a href="#_7-1-1-语法1-指定id" class="header-anchor">#</a> 7.1.1 语法1-指定ID</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>PUT /索引名/_doc/文档ID
{
  field1: value1,
  field2: value2,
  ...
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="_7-1-2-语法2-不指定id"><a href="#_7-1-2-语法2-不指定id" class="header-anchor">#</a> 7.1.2 语法2-不指定ID</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>POST /索引名/_doc
{
  field1: value1,
  field2: value2,
  ...
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>注意：</p> <ul><li><p>不指定指定id默认使用随机字符串，同时语法必须使用POST方式</p></li> <li><p>当索引/映射不存在时，会使用默认设置自动添加</p></li> <li><p>ES中的数据一般是从别的数据库导入的，所以文档的ID会沿用原数据库中的ID</p></li> <li><p>操作时，如果指定文档id， 并且索引库中已经存在， 则执行更新操作， 否则执行添加</p></li></ul> <h4 id="_7-1-3-练习"><a href="#_7-1-3-练习" class="header-anchor">#</a> 7.1.3 练习</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求1：新增一个文档
PUT /my_index/_doc/1
{
  &quot;id&quot;:1,
  &quot;name&quot;:&quot;dafei&quot;,
  &quot;age&quot;:18
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>结果字段解释：</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;_index&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;my_index&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_type&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;_doc&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_id&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_version&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  <span class="token property">&quot;result&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;created&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_shards&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;total&quot;</span> <span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">&quot;successful&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">&quot;failed&quot;</span> <span class="token operator">:</span> <span class="token number">0</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_seq_no&quot;</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_primary_term&quot;</span> <span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><table><thead><tr><th>字段</th> <th>解释</th></tr></thead> <tbody><tr><td>_index</td> <td>所属索引</td></tr> <tr><td>_type</td> <td>所属类型</td></tr> <tr><td>_id</td> <td>文档ID</td></tr> <tr><td>_version</td> <td>乐观锁版本号</td></tr> <tr><td>_source</td> <td>数据内容</td></tr> <tr><td>result</td> <td>命令操作类型</td></tr> <tr><td>_shards</td> <td>分片相关信息</td></tr></tbody></table> <h3 id="_7-2-更新"><a href="#_7-2-更新" class="header-anchor">#</a> 7.2 更新</h3> <h4 id="_7-2-1-语法1-全量更新"><a href="#_7-2-1-语法1-全量更新" class="header-anchor">#</a> 7.2.1 语法1-全量更新</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>PUT /索引名/_doc/文档ID
{
  field1: value1,
  field2: value2,
  ...
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>练习</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求2：替换一个文档
PUT /my_index/_doc/1
{
  &quot;id&quot;:1,
  &quot;name&quot;:&quot;xiaofei&quot;,
   &quot;age&quot;:18
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;_index&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;my_index&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_type&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;users&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_id&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_version&quot;</span> <span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
  <span class="token property">&quot;result&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;updated&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_shards&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;total&quot;</span> <span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
    <span class="token property">&quot;successful&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">&quot;failed&quot;</span> <span class="token operator">:</span> <span class="token number">0</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_seq_no&quot;</span> <span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_primary_term&quot;</span> <span class="token operator">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>全量更新，就是所有字段更新，必须明确指定每一个字段的value值，否则update之后，数据会丢失。</p> <p>执行前：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905190120117.png" alt="image-20220905190120117">*</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token constant">PUT</span> <span class="token operator">/</span>my_index<span class="token operator">/</span>_doc<span class="token operator">/</span><span class="token number">1</span>
<span class="token punctuation">{</span>
  <span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;xiaofei&quot;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>执行后，</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905190059541.png" alt="image-20220905190059541">*</p> <h4 id="_7-2-2-语法2-部分更新"><a href="#_7-2-2-语法2-部分更新" class="header-anchor">#</a> 7.2.2 语法2-部分更新</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>POST /索引名/_update/文档id
{
  &quot;doc&quot;:{
    &quot;要改动字段&quot;:&quot;值&quot;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>练习</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#只改名称
POST /my_index/_update/1/
{
  &quot;doc&quot;:{
    &quot;name&quot;:&quot;xiaofei&quot;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_7-3-查询"><a href="#_7-3-查询" class="header-anchor">#</a> 7.3 查询</h3> <p><strong>语法</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#查单个
GET /索引名/_doc/文档ID
#查所有
GET /索引名/_doc/_search
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>练习</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#查单个
GET /my_index/_doc/1
#查所有
GET /my_index/_search

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>查单个</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;_index&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;my_index&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_type&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;_doc&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_id&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_version&quot;</span> <span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_seq_no&quot;</span> <span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_primary_term&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  <span class="token property">&quot;found&quot;</span> <span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_source&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;id&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">&quot;name&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;xiaofei&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;age&quot;</span> <span class="token operator">:</span> <span class="token number">18</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p><strong>查所有</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;took&quot;</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
  <span class="token property">&quot;timed_out&quot;</span> <span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  <span class="token property">&quot;_shards&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;total&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">&quot;successful&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    <span class="token property">&quot;skipped&quot;</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    <span class="token property">&quot;failed&quot;</span> <span class="token operator">:</span> <span class="token number">0</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token property">&quot;hits&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;total&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token property">&quot;value&quot;</span> <span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
      <span class="token property">&quot;relation&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;eq&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">&quot;max_score&quot;</span> <span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
    <span class="token property">&quot;hits&quot;</span> <span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        <span class="token property">&quot;_index&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;my_index&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_type&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;_doc&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_id&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_score&quot;</span> <span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_source&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
          <span class="token property">&quot;id&quot;</span> <span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
          <span class="token property">&quot;name&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;xiaofei&quot;</span><span class="token punctuation">,</span>
          <span class="token property">&quot;age&quot;</span> <span class="token operator">:</span> <span class="token number">18</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span>
        <span class="token property">&quot;_index&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;my_index&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_type&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;_doc&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_id&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;2&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_score&quot;</span> <span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
        <span class="token property">&quot;_source&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
          <span class="token property">&quot;id&quot;</span> <span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
          <span class="token property">&quot;name&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;dafei&quot;</span><span class="token punctuation">,</span>
          <span class="token property">&quot;age&quot;</span> <span class="token operator">:</span> <span class="token number">18</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><p>结果字段解释：</p> <table><thead><tr><th>字段</th> <th>解释</th></tr></thead> <tbody><tr><td>took</td> <td>耗时</td></tr> <tr><td>_shards.total</td> <td>分片总数</td></tr> <tr><td>hits.total</td> <td>查询到的数量</td></tr> <tr><td>hits.max_score</td> <td>最大匹配度</td></tr> <tr><td>hits.hits</td> <td>查询到的结果</td></tr> <tr><td>hits.hits._score</td> <td>匹配度</td></tr></tbody></table> <h3 id="_7-4-删除"><a href="#_7-4-删除" class="header-anchor">#</a> 7.4 删除</h3> <p><strong>语法</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>DELETE /索引名/类型名/文档ID
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>练习</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求1：根据文档ID删除一个文档
DELETE /my_index/_doc/1

#需求2：又添加刚刚删除的文档
PUT /my_index/_doc/1
{
  &quot;id&quot;:1,
  &quot;name&quot;:&quot;dafei&quot;,
  &quot;age&quot;:18
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>注意：这里的删除并且不是真正意义上的删除，仅仅是清空文档内容而已，并且标记该文档的状态为删除</strong></p> <h2 id="八、全文搜索"><a href="#八、全文搜索" class="header-anchor">#</a> 八、全文搜索</h2> <p>接下来就是做全文搜索啦，搜索前先进行数据初始化。</p> <h3 id="_8-1-数据准备"><a href="#_8-1-数据准备" class="header-anchor">#</a> 8.1 数据准备</h3> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>PUT /product
{
  &quot;mappings&quot;: {
      &quot;properties&quot;: {
        &quot;id&quot;: {
	  &quot;type&quot;: &quot;long&quot;
	},
        &quot;title&quot;: {
          &quot;type&quot;: &quot;text&quot;, 
          &quot;analyzer&quot;: &quot;ik_smart&quot;, 
          &quot;search_analyzer&quot;: &quot;ik_smart&quot;,
          &quot;fields&quot;: {
          &quot;keyword&quot;: {
          &quot;type&quot;: &quot;keyword&quot;,
            &quot;ignore_above&quot;: 256
            }  
          }
        },
        &quot;price&quot;: {&quot;type&quot;: &quot;integer&quot;},
        &quot;intro&quot;: {
          &quot;type&quot;: &quot;text&quot;, 
          &quot;analyzer&quot;: &quot;ik_smart&quot;, 
          &quot;search_analyzer&quot;: &quot;ik_smart&quot;,
          &quot;fields&quot;: {
            &quot;keyword&quot;: {
              &quot;type&quot;: &quot;keyword&quot;,
              &quot;ignore_above&quot;: 256
            }  
          }
        },
      	&quot;brand&quot;: {
      	  &quot;type&quot;: &quot;keyword&quot; 
      	}
      }
  }
}

POST /product/_bulk
{&quot;create&quot;:{&quot;_id&quot;: 1}}
{&quot;id&quot;:1,&quot;title&quot;:&quot;Apple iPhone XR (A2108) 128GB 白色 移动联通电信4G手机 双卡双待&quot;,&quot;price&quot;:5299,&quot;intro&quot;:&quot;【iPhoneXR限时特惠！】6.1英寸视网膜显示屏，A12仿生芯片，面容识别，无线充电，支持双卡！选【换修无忧版】获 AppleCare 原厂服务，享只换不修！更有快速换机、保值换新、轻松月付！&quot;,&quot;brand&quot;:&quot;Apple&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 2}}
{&quot;id&quot;:2,&quot;title&quot;:&quot;Apple 2019款 Macbook Pro 13.3【带触控栏】八代i7 18G 256G RP645显卡 深空灰 苹果笔记本电脑 轻薄本 MUHN2CH/A&quot;,&quot;price&quot;:15299,&quot;intro&quot;:&quot;【八月精选】Pro2019年新品上市送三重好礼，现在购买领满8000减400元优惠神劵，劵后更优惠！&quot;,&quot;brand&quot;:&quot;Apple&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 3}}
{&quot;id&quot;:3,&quot;title&quot;:&quot;Apple iPad Air 3 2019年新款平板电脑 10.5英寸（64G WLAN版/A12芯片/Retina显示屏/MUUL2CH/A）金色&quot;,&quot;price&quot;:3788,&quot;intro&quot;:&quot;8月尊享好礼！买iPad即送蓝牙耳机！领券立减！多款产品支持手写笔！【新一代iPad，总有一款适合你】选【换修无忧版】获 AppleCare 原厂服务，享只换不修！更有快速换机、保值换新、轻松月付！&quot;,&quot;brand&quot;:&quot;Apple&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 4}}
{&quot;id&quot;:4,&quot;title&quot;:&quot;华为HUAWEI MateBook X Pro 2019款 英特尔酷睿i5 13.9英寸全面屏轻薄笔记本电脑(i5 8G 512G 3K 触控) 灰&quot;,&quot;price&quot;:7999,&quot;intro&quot;:&quot;3K全面屏开启无界视野;轻薄设计灵动有型，HuaweiShare一碰传&quot;,&quot;brand&quot;:&quot;华为&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 5}}
{&quot;id&quot;:5,&quot;title&quot;:&quot;华为 HUAWEI Mate20 X (5G) 7nm工艺5G旗舰芯片全面屏超大广角徕卡三摄8GB+256GB翡冷翠5G双模全网通手机&quot;,&quot;price&quot;:6199,&quot;intro&quot;:&quot;【5G双模，支持SA/NSA网络，7.2英寸全景巨屏，石墨烯液冷散热】5G先驱，极速体验。&quot;,&quot;brand&quot;:&quot;华为&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 6}}
{&quot;id&quot;:6,&quot;title&quot;:&quot;华为平板 M6 10.8英寸麒麟980影音娱乐平板电脑4GB+64GB WiFi（香槟金）&quot;,&quot;price&quot;:2299,&quot;intro&quot;:&quot;【华为暑期购】8月2日-4日，M5青春版指定爆款型号优惠100元，AI语音控制&quot;,&quot;brand&quot;:&quot;华为&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 7}}
{&quot;id&quot;:7,&quot;title&quot;:&quot;荣耀20 PRO DXOMARK全球第二高分 4800万四摄 双光学防抖 麒麟980 全网通4G 8GB+128GB 蓝水翡翠 拍照手机&quot;,&quot;price&quot;:3199,&quot;intro&quot;:&quot;白条6期免息！麒麟980，4800万全焦段AI四摄！荣耀20系列2699起，4800万超广角AI四摄！&quot;,&quot;brand&quot;:&quot;荣耀&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 8}}
{&quot;id&quot;:8,&quot;title&quot;:&quot;荣耀MagicBook Pro 16.1英寸全面屏轻薄性能笔记本电脑（酷睿i7 8G 512G MX250 IPS FHD 指纹解锁）冰河银&quot;,&quot;price&quot;:6199,&quot;intro&quot;:&quot;16.1英寸无界全面屏金属轻薄本，100%sRGB色域，全高清IPS防眩光护眼屏，14小时长续航，指纹一健开机登录，魔法一碰传高速传输。&quot;,&quot;brand&quot;:&quot;荣耀&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 9}}
{&quot;id&quot;:9,&quot;title&quot;:&quot;荣耀平板5 麒麟8核芯片 GT游戏加速 4G+128G 10.1英寸全高清屏影音平板电脑 WiFi版 冰川蓝&quot;,&quot;price&quot;:1549,&quot;intro&quot;:&quot;【爆款平板推荐】哈曼卡顿专业调音，10.1英寸全高清大屏，双喇叭立体环绕音，配置多重护眼，值得拥有！&quot;,&quot;brand&quot;:&quot;荣耀&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 10}}
{&quot;id&quot;:10,&quot;title&quot;:&quot;小米9 4800万超广角三摄 6GB+128GB全息幻彩蓝 骁龙855 全网通4G 双卡双待 水滴全面屏拍照智能游戏手机&quot;,&quot;price&quot;:2799,&quot;intro&quot;:&quot;限时优惠200，成交价2799！索尼4800万广角微距三摄，屏下指纹解锁！&quot;,&quot;brand&quot;:&quot;小米&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 11}}
{&quot;id&quot;:11,&quot;title&quot;:&quot;小米(MI)Pro 2019款 15.6英寸金属轻薄笔记本(第八代英特尔酷睿i7-8550U 16G 512GSSD MX250 2G独显) 深空灰&quot;,&quot;price&quot;:6899,&quot;intro&quot;:&quot;【PCIE固态硬盘、72%NTSC高色域全高清屏】B面康宁玻璃覆盖、16G双通道大内存、第八代酷睿I7处理器、专业级调校MX150&quot;,&quot;brand&quot;:&quot;小米&quot;}
{&quot;create&quot;:{&quot;_id&quot;: 12}}
{&quot;id&quot;:12,&quot;title&quot;:&quot;联想(Lenovo)拯救者Y7000P 2019英特尔酷睿i7 15.6英寸游戏笔记本电脑(i7 9750H 16G 1T SSD GTX1660Ti 144Hz)&quot;,&quot;price&quot;:9299,&quot;intro&quot;:&quot;超大1T固态，升级双通道16G内存一步到位，GTX1660Ti电竞级独显，英特尔9代i7H高性能处理器，144Hz电竞屏窄边框！&quot;,&quot;brand&quot;:&quot;联想&quot;}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br></div></div><p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905191804014.png" alt="image-20220905191804014">*</p> <h3 id="_8-2-搜索语句"><a href="#_8-2-搜索语句" class="header-anchor">#</a> 8.2 搜索语句</h3> <p>全文搜索依靠2个语句：match   multi_match</p> <h4 id="_8-2-1-语法1-match"><a href="#_8-2-1-语法1-match" class="header-anchor">#</a> 8.2.1 语法1-match</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>GET /索引/_search
{
  &quot;query&quot;: {
    &quot;match&quot;: {field: value}
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>match 用于单字段全文检索，value值会被分词器拆分，然后去倒排索引中匹配</p> <p>练习</p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求：查询商品标题中符合&quot;游戏 手机&quot;的字样的商品 【暂时理解为模糊查询】
GET /product/_search
{
  &quot;query&quot;: {
    &quot;match&quot;:{
     &quot;title&quot;: &quot;游戏 手机&quot;
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="_8-2-2-语法2-multi-match"><a href="#_8-2-2-语法2-multi-match" class="header-anchor">#</a> 8.2.2 语法2-multi_match</h4> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>GET /索引/_search
{
  &quot;query&quot;: {
    &quot;multi_match&quot;: {
      “query”：关键词语,
      &quot;fields&quot;:[xxxxm,xxx]
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>multi_match用于多字段全文检索，value值会被分词器拆分，然后去倒排索引中匹配</p> <p><strong>练习</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求：查询商品标题或简介中符合&quot;蓝牙 指纹 双卡&quot;的字样的商品
GET /product/_search
{
  &quot;query&quot;: {
    &quot;multi_match&quot;:{
      &quot;query&quot;:&quot;蓝牙 指纹 双卡&quot;,
      &quot;fields&quot;: [&quot;title&quot;, &quot;intro&quot;]
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="_8-3-中文分词器"><a href="#_8-3-中文分词器" class="header-anchor">#</a> 8.3 中文分词器</h3> <p>留意观察上面全文搜索结果，大家会发现，全文搜索出来效果并没有想象那么好，关键词基本是一个字一个字匹配，这是怎么一回事呢？那就涉及到Elasticsearch分词器概念了。</p> <h4 id="_8-3-1-概念"><a href="#_8-3-1-概念" class="header-anchor">#</a> 8.3.1 概念</h4> <p>Elasticsearch在没有明确指定分词器规则时，会使用 默认的standard 分词器对关键词语拆词并匹配。</p> <p><strong>默认分词器分词规则</strong>：按照单词切分，内容转变为小写，去掉标点，如果遇到中文，那就当成1个单词处理（一个字一个字拆分）</p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#默认分词器：
GET /product/_analyze
{
  &quot;text&quot;:&quot;I am Groot&quot;
}
GET /product/_analyze
{
  &quot;text&quot;:&quot;英特尔酷睿i7处理器&quot;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>很明显，上面的分词无法满足中文的分词，那怎么办？使用第三方：ik中文分词器</p> <p>安装方式，在： Elasticsearch 安装章节讲过了，这里不重复了。</p> <h4 id="_8-3-2-分词策略"><a href="#_8-3-2-分词策略" class="header-anchor">#</a> 8.3.2 分词策略</h4> <p>IK分词器有2种分词策略，操作时必须明确指定。</p> <p><strong>1、ik_max_word 【细粒度分词】</strong></p> <p>会将文本做最细粒度的拆分，比如会将“中华人民共和国人民大会堂”拆分为“中华人民共和国、中华人民、中华、华人、人民共和国、人民、共和国、大会堂、大会、会堂等词语。</p> <p><strong>2、ik_smart 【粗粒度分词】</strong></p> <p>会做最粗粒度的拆分，比如会将“中华人民共和国人民大会堂”拆分为中华人民共和国、人民大会堂。</p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>GET /product/_analyze
{
  &quot;text&quot;:&quot;I am Groot&quot;,
  &quot;analyzer&quot;:&quot;ik_smart&quot;
}

GET /product/_analyze
{
  &quot;text&quot;:&quot;英特尔酷睿i7处理器&quot;,
  &quot;analyzer&quot;:&quot;ik_smart&quot;
}

GET /product/_analyze
{
  &quot;text&quot;:&quot;英特尔酷睿i7处理器&quot;,
  &quot;analyzer&quot;:&quot;ik_max_word&quot;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>从效果上看，这分词就正常多了。</p> <h4 id="_8-3-3-分词词库"><a href="#_8-3-3-分词词库" class="header-anchor">#</a> 8.3.3 分词词库</h4> <p>分词完之后，不禁会想，IK分词怎么做到的将长句拆分成词语的？那输入是  <strong>淘宝</strong>  会不会被拆分，<strong>叩丁狼</strong> 会不会被拆分？</p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>GET /product/_analyze
{
  &quot;text&quot;:&quot;淘宝&quot;,
  &quot;analyzer&quot;:&quot;ik_smart&quot;
}

GET /product/_analyze
{
  &quot;text&quot;:&quot;叩丁狼&quot;,
  &quot;analyzer&quot;:&quot;ik_max_word&quot;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>答案很显然，<strong>淘宝</strong>不会被分， <strong>叩丁狼</strong>会被分。为啥？因为ik分词器的词库中有<strong>淘宝</strong>，而没有<strong>叩丁狼</strong></p> <p>打开：<strong>config/main.dic</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905193129240.png" alt="image-20220905193129240">*</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905193152213.png" alt="image-20220905193152213">*</p> <p>到这真相大白，只要<strong>词库维护好，IK自然好</strong></p> <h3 id="_8-4-倒排索引"><a href="#_8-4-倒排索引" class="header-anchor">#</a> 8.4 倒排索引</h3> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905193528278.png" alt="image-20220905193528278">*</p> <p>具体描述</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905193649024.png" alt="image-20220905193649024"></p> <p><strong>正排索引：先查询数据，然后筛选条件</strong></p> <p><strong>倒排索引：先筛选条件，再查询数据</strong></p> <h3 id="_8-5-高亮显示"><a href="#_8-5-高亮显示" class="header-anchor">#</a> 8.5 高亮显示</h3> <p><strong>效果</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905193854998.png" alt="image-20220905193854998">*</p> <p><strong>原理：所谓的高亮显示就是给全搜索匹配关键词加上高亮样式(加粗/改颜色)</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20220905194146680.png" alt="image-20220905194146680"></p> <p><strong>语法</strong></p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>GET /索引/_search
{
  &quot;query&quot;: { ... },
  &quot;highlight&quot;: {
    &quot;fields&quot;: {
    &quot;field1&quot;: {},
    &quot;field2&quot;: {},
     ...
    },
    &quot;pre_tags&quot;: 开始标签,
    &quot;post_tags&quot; 结束标签
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>highlight：表示高亮显示，需要在fields中配置哪些字段中检索到该内容需要高亮显示</p> <p>必须配合 match/multi_match一起使用</p> <p>练习</p> <div class="language-asciiarmor line-numbers-mode"><pre class="language-text"><code>#需求：查询商品标题或简介中符合&quot;蓝牙 指纹 双卡&quot;的字样的商品，并且高亮显示
GET /product/_search
{
  &quot;query&quot;: {
    &quot;multi_match&quot;:{
      &quot;query&quot;:&quot;蓝牙 指纹 双卡&quot;,
      &quot;fields&quot;: [&quot;title&quot;, &quot;intro&quot;],
      &quot;analyzer&quot;: &quot;ik_smart&quot;
    }
  },
  &quot;highlight&quot;: {
    &quot;fields&quot;: {
      &quot;title&quot;: {&quot;type&quot;: &quot;plain&quot;},
      &quot;intro&quot;: {&quot;type&quot;: &quot;plain&quot;}
    },
    &quot;pre_tags&quot;: &quot;&lt;span style='color:red;'&gt;&quot;,
    &quot;post_tags&quot;: &quot;&lt;/span&gt;&quot;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h2 id="九、elasticsearch编程"><a href="#九、elasticsearch编程" class="header-anchor">#</a> 九、Elasticsearch编程</h2> <p>这里使用SpringBoot的方式实现Elasticsearch编程操作</p> <h3 id="_9-1-crud操作"><a href="#_9-1-crud操作" class="header-anchor">#</a> 9.1 CRUD操作</h3> <p>Elasticsearch作为数据库，那肯定离不开CRUD的操作啦。</p> <p><strong>步骤1：创建标准的SpringBoot项目：es-demo</strong></p> <p><strong>步骤2：导入相关依赖</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.4.3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-data-elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p><strong>步骤3：配置ES</strong></p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token comment">#application.properties</span>
<span class="token key attr-name">spring.elasticsearch.rest.uris</span><span class="token punctuation">=</span><span class="token value attr-value">http://localhost:9200</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>步骤4：编写实体类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Getter</span>
<span class="token annotation punctuation">@Setter</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token annotation punctuation">@NoArgsConstructor</span>
<span class="token annotation punctuation">@AllArgsConstructor</span>
<span class="token annotation punctuation">@Document</span><span class="token punctuation">(</span>indexName<span class="token operator">=</span><span class="token string">&quot;product&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Product</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Id</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> id<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>analyzer<span class="token operator">=</span><span class="token string">&quot;ik_smart&quot;</span><span class="token punctuation">,</span>searchAnalyzer<span class="token operator">=</span><span class="token string">&quot;ik_smart&quot;</span><span class="token punctuation">,</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Text</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> title<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> price<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>analyzer<span class="token operator">=</span><span class="token string">&quot;ik_smart&quot;</span><span class="token punctuation">,</span>searchAnalyzer<span class="token operator">=</span><span class="token string">&quot;ik_smart&quot;</span><span class="token punctuation">,</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Text</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> intro<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type<span class="token operator">=</span><span class="token class-name">FieldType<span class="token punctuation">.</span>Keyword</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> brand<span class="token punctuation">;</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p><strong>步骤5：编写持久层代码</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
* 1&gt;自定义接口继承ElasticsearchRepository
* 2&gt;明确指定2个泛型： 
*   1：当前接口操作实体类： product
*   2：当前接口操作实体类主键类型：String
*
*/</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ProductRepository</span>  <span class="token keyword">extends</span> <span class="token class-name">ElasticsearchRepository</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>步骤6：编写Service接口与实现类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">IProductService</span> <span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">Product</span> product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Product</span> product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Product</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> <span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProductServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">IProductService</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ProductRepository</span> repository<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ElasticsearchRestTemplate</span>  template<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">Product</span> product<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        repository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Product</span> product<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        repository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        repository<span class="token punctuation">.</span><span class="token function">deleteById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Product</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> repository<span class="token punctuation">.</span><span class="token function">findById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> <span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Iterable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> all <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        all<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>a<span class="token operator">-&gt;</span>p<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> p<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p><strong>步骤7：编写测试类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ElasticsearchDemoApplicationTests</span> <span class="token punctuation">{</span>
	<span class="token annotation punctuation">@Autowired</span>
	<span class="token keyword">private</span> <span class="token class-name">IProductService</span> productService<span class="token punctuation">;</span>
	<span class="token annotation punctuation">@Test</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSave</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Product</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Product</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        p<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token string">&quot;123&quot;</span><span class="token punctuation">)</span>
		p<span class="token punctuation">.</span><span class="token function">setBrand</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setIntro</span><span class="token punctuation">(</span><span class="token string">&quot;dafei手机&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">&quot;全球最帅的手机&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		productService<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token annotation punctuation">@Test</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testUpate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Product</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Product</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token string">&quot;ue5r1m4BXlaPW5P0TegF&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setBrand</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setIntro</span><span class="token punctuation">(</span><span class="token string">&quot;dafei手机&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">&quot;全球最sb的手机&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		productService<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token annotation punctuation">@Test</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testDelete</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		productService<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string">&quot;123&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token annotation punctuation">@Test</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testGet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>productService<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;123&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token annotation punctuation">@Test</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>err<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>productService<span class="token punctuation">.</span><span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h3 id="_9-2-全文搜索"><a href="#_9-2-全文搜索" class="header-anchor">#</a> 9.2 全文搜索</h3> <p><strong>步骤1：新建类：QueryTest</strong></p> <p><strong>步骤2：编写2个全文搜索方法</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 查询商品标题中符合&quot;游戏 手机&quot;的字样的商品</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testQuery4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">NativeSearchQueryBuilder</span> builder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NativeSearchQueryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/**
         * {
         *     query:{
         *         match:{title:&quot;游戏 手机&quot;}
         *     }
         * }
         */</span>
    builder<span class="token punctuation">.</span><span class="token function">withQuery</span><span class="token punctuation">(</span>
        <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">&quot;title&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;游戏 手机&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    builder<span class="token punctuation">.</span><span class="token function">withPageable</span><span class="token punctuation">(</span><span class="token class-name">PageRequest</span><span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Page</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> search <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>builder<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    search<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>err<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 查询商品标题或简介中符合&quot;蓝牙 指纹 双卡&quot;的字样的商品</span>
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testQuery7</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">NativeSearchQueryBuilder</span> builder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NativeSearchQueryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/**
         * {
         *     query:{
         *         multi_match:{
         *             &quot;query&quot;:&quot;蓝牙 指纹 双卡&quot;,
         *             &quot;fields&quot;:[&quot;title&quot;, &quot;intro&quot;]
         *         }
         *     }
         * }
         */</span>
    builder<span class="token punctuation">.</span><span class="token function">withQuery</span><span class="token punctuation">(</span>
        <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">multiMatchQuery</span><span class="token punctuation">(</span><span class="token string">&quot;蓝牙 指纹 双卡&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;title&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;intro&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    builder<span class="token punctuation">.</span><span class="token function">withPageable</span><span class="token punctuation">(</span><span class="token class-name">PageRequest</span><span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token class-name">Sort<span class="token punctuation">.</span>Direction</span><span class="token punctuation">.</span><span class="token constant">DESC</span><span class="token punctuation">,</span> <span class="token string">&quot;price&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Page</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> search <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>builder<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    search<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>err<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_9-3-高亮显示-拓展"><a href="#_9-3-高亮显示-拓展" class="header-anchor">#</a> 9.3 高亮显示[拓展]</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testHighlight</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment">//定义索引库</span>
    <span class="token class-name">SearchRequest</span> searchRequest <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token string">&quot;product&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">SearchSourceBuilder</span> searchSourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//定义query查询</span>
    <span class="token class-name">MultiMatchQueryBuilder</span> queryBuilder <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">multiMatchQuery</span><span class="token punctuation">(</span><span class="token string">&quot;蓝牙 指纹 双卡&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;title&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;intro&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">HighlightBuilder</span> highlightBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HighlightBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 生成高亮查询器</span>
    highlightBuilder<span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;title&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 高亮查询字段</span>
    highlightBuilder<span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;intro&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 高亮查询字段</span>
    highlightBuilder<span class="token punctuation">.</span><span class="token function">requireFieldMatch</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 如果要多个字段高亮,这项要为false</span>
    highlightBuilder<span class="token punctuation">.</span><span class="token function">preTags</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;span style='color:red'&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 高亮设置</span>
    highlightBuilder<span class="token punctuation">.</span><span class="token function">postTags</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;/span&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    highlightBuilder<span class="token punctuation">.</span><span class="token function">fragmentSize</span><span class="token punctuation">(</span><span class="token number">800000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 最大高亮分片数</span>
    highlightBuilder<span class="token punctuation">.</span><span class="token function">numOfFragments</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 从第一个分片获取高亮片段</span>
    <span class="token class-name">Pageable</span> pageable <span class="token operator">=</span> <span class="token class-name">PageRequest</span><span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 设置分页参数</span>
    <span class="token class-name">NativeSearchQuery</span> searchQuery <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NativeSearchQueryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">withQuery</span><span class="token punctuation">(</span>queryBuilder<span class="token punctuation">)</span> <span class="token comment">// match查询</span>
        <span class="token punctuation">.</span><span class="token function">withPageable</span><span class="token punctuation">(</span>pageable<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">withHighlightBuilder</span><span class="token punctuation">(</span>highlightBuilder<span class="token punctuation">)</span> <span class="token comment">// 设置高亮</span>
        <span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">SearchHits</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> searchHits <span class="token operator">=</span> template<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>searchQuery<span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> searchHit <span class="token operator">:</span> searchHits<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 获取搜索到的数据</span>
        <span class="token class-name">Product</span> content <span class="token operator">=</span> searchHit<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 处理高亮</span>
        <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> highlightFields <span class="token operator">=</span> searchHit<span class="token punctuation">.</span><span class="token function">getHighlightFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Map<span class="token punctuation">.</span>Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> stringHighlightFieldEntry <span class="token operator">:</span> highlightFields<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">String</span> key <span class="token operator">=</span> stringHighlightFieldEntry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token string">&quot;title&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> fragments <span class="token operator">=</span> stringHighlightFieldEntry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">StringBuilder</span> sb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> fragment <span class="token operator">:</span> fragments<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    sb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>fragment<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                content<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span>sb<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token string">&quot;intro&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> fragments <span class="token operator">=</span> stringHighlightFieldEntry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">StringBuilder</span> sb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> fragment <span class="token operator">:</span> fragments<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    sb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>fragment<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                content<span class="token punctuation">.</span><span class="token function">setIntro</span><span class="token punctuation">(</span>sb<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">Page</span> page <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PageImpl</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> pageable<span class="token punctuation">,</span> searchHits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    list<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br></div></div><h2 id="十、小结与作业"><a href="#十、小结与作业" class="header-anchor">#</a> 十、小结与作业</h2> <ul><li>elasticsearch 概念, 定位,优点,缺点,运用 跟mysql, redis mongodb es 两两对比</li> <li>elasticsearch 安装</li> <li>elasticsearch 索引操作</li> <li>elasticsearch 文档操作</li> <li>elasticsearch 全文搜索</li> <li>springboot集成elasticsearch--CRUD --全文搜索</li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=NoSQL" title="标签">#NoSQL</a><a href="/tags/?tag=ElasticSearch" title="标签">#ElasticSearch</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/4c087b/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">MongoDB</div></a> <a href="/pages/2ea428/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">SpringCloud Alibaba</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/4c087b/" class="prev">MongoDB</a></span> <span class="next"><a href="/pages/2ea428/">SpringCloud Alibaba</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/62.a9d3b3b7.js" defer></script>
  </body>
</html>
